package kr.co.mcshop.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import kr.co.mcshop.qna.QnA;
import kr.co.mcshop.qna.QnAService;
import kr.co.mcshop.qnacomment.QnAComment;
import kr.co.mcshop.qnacomment.QnACommentService;
import kr.co.mcshop.qnareply.QnAReply;
import kr.co.mcshop.qnareply.QnAReplyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping(value="/admin")
@Controller
public class AdminQnAController {
	@Autowired
	private QnAService qnaService;
	
	@Autowired
	private QnACommentService qnacommentService;

	@Autowired
	private QnAReplyService qnareplyService;
	
	@RequestMapping(value = "/FAQ", method = RequestMethod.GET)
	public String FAQ(Model model, @RequestParam Map map) {
		List<QnA> list = qnaService.selectqna(map);
		model.addAttribute("qnaList", list);
		return "/admin/faq/FAQ";
	}
	
	@RequestMapping(value = "/FAQ_detail", method = RequestMethod.GET)
	public String qna_detail(Model model, @RequestParam Map map) {

		qnaService.updateReadCount(map);
		List<QnA> list = qnaService.selectqna(map);
		model.addAttribute("qnaList", list.get(0));
		
		ArrayList<QnAComment> list1 = qnacommentService.selectqnacomment(map);
		model.addAttribute("commentList", list1);
		
		ArrayList<QnAReply> list2 = new ArrayList<QnAReply>();
			
		for(int i=0; i<list1.size(); i++){
				for(int j=0; j<qnareplyService.selectreply(list1.get(i).getComment_no()).size(); j++){
					list2.addAll(j, qnareplyService.selectreply(list1.get(j).getComment_no()));
				}
		}
		model.addAttribute("replyList", list2);
		return "admin/faq/FAQ_detail";
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	public String search(String board_title, Model model, @RequestParam Map map) {
	    ArrayList<QnA> list = new ArrayList<QnA>();
		if (map.get("searchtxt") == null) {
			return "redirect:FAQ";
		}
		if(map.get("search").equals("0")){
			map.put("searchtxt", map.get("searchtxt"));
			List<QnA> titleSearchResult = qnaService.searchtitle(map);
			
			for(QnA qnalist : titleSearchResult){
				list.add(qnalist);
			}
		}else if(map.get("search").equals("1")){
			map.put("searchtxt", map.get("searchtxt"));
			List<QnA> userIdSearchResult = qnaService.searchuserid(map);
			
			for(QnA qnalist : userIdSearchResult){
				list.add(qnalist);
			}
		}
		model.addAttribute("qnaList", list);
		return "admin/faq/FAQ";
	}
	@RequestMapping(value = "Delete", method = RequestMethod.GET)
	public String delete(Model model, @RequestParam Map map) {
		qnaService.deleteqna(map);
		qnacommentService.faqcommentDelete(map);
		qnareplyService.FAQreplyDelete(map);
		return "redirect:FAQ";
	}
	
	@RequestMapping(value = "/FAQcommentDelete", method = RequestMethod.GET)
	public String qnacommentDelete(Model model, @RequestParam Map map) {
		System.out.println(map);
		qnacommentService.FAQcommentDelete(map);
		qnareplyService.replyDelete(map);
		return "redirect:admin/faq/FAQ_detail";
	}
		
	@RequestMapping(value = "/faqreplyDelete", method = RequestMethod.GET)
	public String faqreplyDelete(Model model, @RequestParam Map map) {
		System.out.println(map);
		qnareplyService.faqreplyDelete(map);
		return "redirect: redirect:admin/faq/FAQ_detail";
	}
}
